package com.pro_test.procenter.controller;


import com.pro_test.commonutils.JwtUtils;
import com.pro_test.commonutils.R;
import com.pro_test.procenter.entity.UcenterMember;
import com.pro_test.procenter.entity.vo.LoginVo;
import com.pro_test.procenter.entity.vo.RegisterVo;
import com.pro_test.procenter.service.UcenterMemberService;
import com.pro_test.servicebase.exceptionhandler.TestException;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author Captain
 * @since 2022-03-04
 */
@RestController
@RequestMapping("/procenter/member")
@CrossOrigin
public class UcenterMemberController {

        @Autowired
        private UcenterMemberService ucenterMemberService;

        @ApiOperation(value = "会员登录")
        @PostMapping("login")
        public R login(@RequestBody LoginVo loginVo) {
            String token = ucenterMemberService.login(loginVo);
            return R.ok().data("token", token).data("mobile",loginVo.getMobile());
        }

        @ApiOperation(value = "会员注册")
        @PostMapping("register")
        public R register(@RequestBody RegisterVo registerVo){
            ucenterMemberService.register(registerVo);
            return R.ok();
        }

    @ApiOperation(value = "根据token获取登录信息")
    @GetMapping("getLoginInfo")
    public R getLoginInfo(HttpServletRequest request){
            String memberId = JwtUtils.getMemberIdByJwtToken(request);
            UcenterMember member = ucenterMemberService.getById(memberId);
            return R.ok().data("userInfo", member);
    }
}

